package Thread.Thread_Security;


public class Demo19 {
    private  static int sum1=0;
    private  static int sum2=0;
    public static void main(String[] args) throws InterruptedException {
        long beginTime=System.currentTimeMillis();
        int[] nums=new int[1000];
        for (int i = 0; i < nums.length; i++) {
            nums[i]=i+1;
        }
        //多个线程修改不同的变量不会出现线程不安全的问题，
        // 只有当多个线程对同一个变量进行操作才会出现线程不安全的问题
        Thread t1=new Thread(()->{
            for (int i = 0; i < nums.length; i+=2) {
                sum1+=nums[i];
            }
        });
        Thread t2=new Thread(()->{
            for (int i = 1; i < nums.length; i+=2) {
                sum2+=nums[i];
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("偶数和："+sum1);
        System.out.println("奇数和："+sum2);
        System.out.println("总和："+(sum1+sum2));
        long endTime=System.currentTimeMillis();
        System.out.println("程序运行时间："+(endTime-beginTime)+"ms");
    }
}
